package com.antis.olsl.library.dialog;

import android.app.Dialog;
import android.content.Context;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.antis.olsl.library.R;
import com.antis.olsl.library.adapter.ChooseSalesroomAdapter;
import com.antis.olsl.library.response.SalesroomOrWarehouseInfo;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;

import java.util.ArrayList;
import java.util.HashMap;

/**
 * @ProjectName: ATS_OLSL
 * @Package: com.antis.olsl.dialog
 * @ClassName: ChooseFiledTypeDialog
 * @Description: 选择门店或者仓库
 * @Author: 羊琪
 * @CreateDate: 2020/3/24 16:25
 * @UpdateDate: 2020/3/24 16:25
 * @UpdateRemark: 更新说明
 * @Version: 1.0
 */
public class ChooseSalesroomsOrWarehouseDialog {
    Context context;
    ArrayList<SalesroomOrWarehouseInfo> salesroomOrWarehouseInfos = new ArrayList<>();
    ArrayList<SalesroomOrWarehouseInfo> chooseSalesroomOrWarehouseInfos = new ArrayList<>();
    ArrayList<SalesroomOrWarehouseInfo> allInfos = new ArrayList<>();
    ChooseSalesroomOrWarehouseListener listener;
    Dialog dialog;
    TextView tvSure;
    RelativeLayout rlChooseAll;
    ImageView imgChoose;
    RecyclerView recyclerView;
    EditText etSearch;

    boolean chooseAll;
    ChooseSalesroomAdapter chooseSalesroomAdapter;
    HashMap<Integer, Integer> map = new HashMap<>();//用来存储过滤数据与原数据对应下标
    int type = 0;//0 门店 1 仓库
    int selectedIndex = -1;

    public ChooseSalesroomsOrWarehouseDialog(Context context, int type, ArrayList<SalesroomOrWarehouseInfo> salesroomOrWarehouseInfos, ChooseSalesroomOrWarehouseListener listener) {
        if (salesroomOrWarehouseInfos == null) {
            return;
        }
        this.type = type;
        this.context = context;
        this.salesroomOrWarehouseInfos.addAll(salesroomOrWarehouseInfos);
        this.allInfos.addAll(salesroomOrWarehouseInfos);
        this.listener = listener;

        initView();
    }

    private void initView() {
        dialog = new Dialog(context, R.style.dialog);
        View view = LayoutInflater.from(context).inflate(R.layout.dialog_choose_salesroom_or_warehouse, null);
        TextView tvTitle = (TextView) view.findViewById(R.id.tv_title);
        TextView tvChooseAllTip = (TextView) view.findViewById(R.id.tv_chooseAllTip);
        imgChoose = (ImageView) view.findViewById(R.id.img_choose);
        etSearch = (EditText) view.findViewById(R.id.et_search);

        recyclerView = (RecyclerView) view.findViewById(R.id.recyclerView);
        chooseSalesroomAdapter = new ChooseSalesroomAdapter(salesroomOrWarehouseInfos);
        recyclerView.setLayoutManager(new LinearLayoutManager(context));
        recyclerView.setAdapter(chooseSalesroomAdapter);

        chooseSalesroomAdapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                if (selectedIndex == position) {
                    return;
                }
                if (selectedIndex != -1) {
                    salesroomOrWarehouseInfos.get(selectedIndex).setChoose(false);
                }
                salesroomOrWarehouseInfos.get(position).setChoose(!salesroomOrWarehouseInfos.get(position).isChoose());
                if (!TextUtils.isEmpty(etSearch.getText().toString()) && map.containsKey(position)) {
                    allInfos.get(map.get(position)).setChoose(salesroomOrWarehouseInfos.get(position).isChoose());
                } else {
                    allInfos.get(position).setChoose(salesroomOrWarehouseInfos.get(position).isChoose());
                }
                selectedIndex = position;
                changeDataStatus();
            }
        });

        tvTitle.setText(type == 0 ? "选择门店" : "选择仓库");
        tvChooseAllTip.setText(type == 0 ? "全部门店" : "全部仓库");

        etSearch.setHint(type == 0 ? "输入门店名称搜索" : "输入仓库名称搜索");


        etSearch.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void afterTextChanged(Editable editable) {
                filterData(etSearch.getText().toString());
            }
        });


        dialog.setContentView(view);
        Window win = dialog.getWindow();
        win.getDecorView().setPadding(0, 0, 0, 0);
        WindowManager.LayoutParams lp = win.getAttributes();
        lp.width = WindowManager.LayoutParams.FILL_PARENT;
//        lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
        win.setAttributes(lp);
        rlChooseAll = (RelativeLayout) view.findViewById(R.id.rl_chooseAll);
        rlChooseAll.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                setAllChooseData();
            }
        });

        view.findViewById(R.id.tv_cancel).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                dialog.dismiss();
            }
        });
        view.findViewById(R.id.ll_top).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                dialog.dismiss();
            }
        });

        tvSure = (TextView) view.findViewById(R.id.tv_sure);
        tvSure.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (listener != null) {
                    listener.onChooseSalesroomOrWarehouseListener(chooseAll, chooseSalesroomOrWarehouseInfos, allInfos, selectedIndex);
                }
                dialog.dismiss();
            }
        });

        dialog.show();
        changeDataStatus();
    }

    /**
     * 修改被选中状态
     */
    private void changeDataStatus() {
        chooseSalesroomOrWarehouseInfos.clear();
        chooseAll = true;
        String chooseId = "";
        for (int i = 0; i < salesroomOrWarehouseInfos.size(); i++) {
            if (salesroomOrWarehouseInfos.get(i).isChoose()) {
                selectedIndex = i;
                chooseSalesroomOrWarehouseInfos.add(salesroomOrWarehouseInfos.get(i));
                chooseAll = false;
                chooseId = salesroomOrWarehouseInfos.get(i).getId();
            }
        }

        for (int j = 0; j < allInfos.size(); j++) {
            if (!TextUtils.isEmpty(chooseId) && chooseId.equals(allInfos.get(j).getId())) {
                allInfos.get(j).setChoose(true);
            } else {
                allInfos.get(j).setChoose(false);
            }
        }

        imgChoose.setSelected(chooseAll);
        chooseSalesroomAdapter.setList(salesroomOrWarehouseInfos);
    }

    /**
     * 根据关键字过滤显示数据
     *
     * @param key
     */
    private void filterData(String key) {
        if (TextUtils.isEmpty(key)) {
            map.clear();
            salesroomOrWarehouseInfos.clear();
            salesroomOrWarehouseInfos.addAll(allInfos);
        } else {
            salesroomOrWarehouseInfos.clear();
            selectedIndex = -1;
            int j = 0;
            for (int i = 0; i < allInfos.size(); i++) {
                if (allInfos.get(i).getName().contains(key)) {
                    if (allInfos.get(i).isChoose()) {
                        selectedIndex = j;
                    }
                    map.put(j++, i);

                    salesroomOrWarehouseInfos.add(allInfos.get(i));
                }
            }
        }
        chooseSalesroomAdapter.setList(salesroomOrWarehouseInfos);
    }

    /**
     * 全选/全不选
     */
    private void setAllChooseData() {
        if (chooseAll) {
            return;
        }
        chooseAll = true;
        chooseSalesroomOrWarehouseInfos.clear();
        for (int i = 0; i < salesroomOrWarehouseInfos.size(); i++) {
            salesroomOrWarehouseInfos.get(i).setChoose(false);
            if (!TextUtils.isEmpty(etSearch.getText().toString()) && map.containsKey(i)) {
                allInfos.get(map.get(i)).setChoose(false);
            }
        }
        selectedIndex = -1;
        imgChoose.setSelected(chooseAll);
        chooseSalesroomAdapter.setList(salesroomOrWarehouseInfos);
    }

    public void dismiss() {
        dialog.dismiss();
    }

    public interface ChooseSalesroomOrWarehouseListener {
        /**
         * @param choosedFiledInfos 已过滤的被选择的数组
         * @param sourceInofs       //包含选择与未选择的原数组
         */
        void onChooseSalesroomOrWarehouseListener(boolean isChooseAll, ArrayList<SalesroomOrWarehouseInfo> choosedFiledInfos, ArrayList<SalesroomOrWarehouseInfo> sourceInofs, int selectedIndex);
    }

}
